Passed
Push — master ( 6f8171...7c2bcd )
by
unknown
48s
created

LookupAPI.getLookupData   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 1
rs 10
1
var LookupAPI = {};
2
3
LookupAPI.lookupMap = {};
4
5
LookupAPI.loadLookupData = function () {
6
    //DivisionAPI.getDivisions(locale);
7
    //BranchAPI.getBranches(locale);
8
    var locales = ["en_CA", "fr_CA"];
9
    //var lookupTypes = ["department", "branch", "division", "province", "jobterm"];
10
    var lookupTypes = ["department", "province", "jobterm", "skill_level", "experience_level", "clearance", "language"];
11
    for (i in locales) {
0 ignored issues
show
Bug introduced by
The variable i seems to be never declared. Assigning variables without defining them first makes them global. If this was intended, consider making it explicit like using window.i.
Loading history...
Coding Style introduced by
Creating global 'for' variable. Should be 'for (var i ...'.
Loading history...
12
        for (j in lookupTypes) {
0 ignored issues
show
Bug introduced by
The variable j seems to be never declared. Assigning variables without defining them first makes them global. If this was intended, consider making it explicit like using window.j.
Loading history...
Coding Style introduced by
Creating global 'for' variable. Should be 'for (var j ...'.
Loading history...
13
            var locale = locales[i];
14
            var lookupType = lookupTypes[j];
15
            LookupAPI.getLookupData(lookupType, locale, null);
16
        }
17
    }
18
};
19
20
21
LookupAPI.getLookupData = function (lookupType, locale, requestCallback) {
22
    var lookup_URL = DataAPI.baseURL + "/" + locale + "/Lookup/" + lookupType;
0 ignored issues
show
Bug introduced by
The variable DataAPI seems to be never declared. If this is a global, consider adding a /** global: DataAPI */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
23
    DataAPI.sendRequest(lookup_URL, 'GET', {}, null, function(request){
24
        LookupAPI.addToLookupMap(lookupType, locale, request.response);
25
        if (requestCallback) {
26
            requestCallback(request);
27
        }
28
    });
29
};
30
31
LookupAPI.addToLookupMap = function (lookupType, locale, response) {
32
    if (!LookupAPI.lookupMap[locale]) {
33
        LookupAPI.lookupMap[locale] = {};
34
    }
35
    if (!LookupAPI.lookupMap[locale][lookupType]) {
36
        LookupAPI.lookupMap[locale][lookupType] = {};
37
    }
38
    LookupAPI.lookupMap[locale][lookupType] = JSON.parse(response);
39
};
40
41
/**
42
 * If the requested lookupType is already in the lookupMap, send it to the lookupCallback
43
 * immediately. If not, request the lookup data, then call lookupCallback with
44
 * the response.
45
 * 
46
 * @param {string} lookupType
47
 * @param {function} lookupCallback
48
 * @return {undefined}
49
 */
50
LookupAPI.getLookupResponse = function (lookupType, lookupCallback) {
51
    var locale = TalentCloudAPI.getLanguageFromCookie();
0 ignored issues
show
Bug introduced by
The variable TalentCloudAPI seems to be never declared. If this is a global, consider adding a /** global: TalentCloudAPI */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
52
    if (!LookupAPI.lookupMap[locale]) {
53
        LookupAPI.lookupMap[locale] = {};
54
    }
55
    if (LookupAPI.lookupMap[locale][lookupType]) {
56
        lookupCallback(LookupAPI.lookupMap[locale][lookupType]);
57
    } else {
58
        LookupAPI.getLookupData(lookupType, locale, function (request) {
59
            if (request.status === 200) {
60
                lookupCallback(LookupAPI.lookupMap[locale][lookupType]);
61
            }
62
        });
63
    }
64
};
65
66
LookupAPI.getLocalizedLookupValue = function (lookupType, valueId) {
67
    var locale = TalentCloudAPI.getLanguageFromCookie();
0 ignored issues
show
Bug introduced by
The variable TalentCloudAPI seems to be never declared. If this is a global, consider adding a /** global: TalentCloudAPI */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
68
    if (!LookupAPI.lookupMap[locale]) {
69
        LookupAPI.lookupMap[locale] = {};
70
    }
71
    var elements = LookupAPI.lookupMap[locale][lookupType];
72
    if (elements) {
73
        for (i in elements) {
0 ignored issues
show
Bug introduced by
The variable i seems to be never declared. Assigning variables without defining them first makes them global. If this was intended, consider making it explicit like using window.i.
Loading history...
Coding Style introduced by
Creating global 'for' variable. Should be 'for (var i ...'.
Loading history...
74
            if (elements[i].id == valueId) {
75
                return elements[i].value;
76
            }
77
        }
78
    } else {
79
        LookupAPI.getLookupData(lookupType, locale, function (request) {
80
            if (request.status === 200 && LookupAPI.lookupMap[locale][lookupType]) {
0 ignored issues
show
Complexity Best Practice introduced by
There is no return statement if request.status === 200 &...upMap.locale.lookupType is false. Are you sure this is correct? If so, consider adding return; explicitly.

This check looks for functions where a return statement is found in some execution paths, but not in all.

Consider this little piece of code

function isBig(a) {
    if (a > 5000) {
        return "yes";
    }
}

console.log(isBig(5001)); //returns yes
console.log(isBig(42)); //returns undefined

The function isBig will only return a specific value when its parameter is bigger than 5000. In any other case, it will implicitly return undefined.

This behaviour may not be what you had intended. In any case, you can add a return undefined to the other execution path to make the return value explicit.

Loading history...
81
                elements = LookupAPI.lookupMap[locale][lookupType];
82
                for (i in elements) {
0 ignored issues
show
Best Practice introduced by
There is no return statement in this branch, but you do return something in other branches. Did you maybe miss it? If you do not want to return anything, consider adding return undefined; explicitly.
Loading history...
Bug introduced by
The variable i seems to be never declared. Assigning variables without defining them first makes them global. If this was intended, consider making it explicit like using window.i.
Loading history...
Coding Style introduced by
Creating global 'for' variable. Should be 'for (var i ...'.
Loading history...
83
                    if (elements[i].id == valueId) {
84
                        return elements[i].value;
85
                    }
86
                }
87
            }
88
        });
89
    }
90
91
    return null;
92
};
93
94
// populates elements passed by element id
95
LookupAPI.populateDropdown = function (lookupType, elementId, useLookupValueAsOptionValue) {
96
    var selectElem = document.getElementById(elementId);
97
    if (selectElem) {
98
        var locale = TalentCloudAPI.getLanguageFromCookie();
0 ignored issues
show
Bug introduced by
The variable TalentCloudAPI seems to be never declared. If this is a global, consider adding a /** global: TalentCloudAPI */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
99
        if (!LookupAPI.lookupMap[locale]) {
100
            LookupAPI.lookupMap[locale] = {};
101
        }
102
        var lookupList = LookupAPI.lookupMap[locale][lookupType];
103
        if (lookupList) {
104
            Utilities.clearSelectOptions(selectElem);
0 ignored issues
show
Bug introduced by
The variable Utilities seems to be never declared. If this is a global, consider adding a /** global: Utilities */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
105
            for (var item in lookupList) {
0 ignored issues
show
Complexity introduced by
A for in loop automatically includes the property of any prototype object, consider checking the key using hasOwnProperty.

When iterating over the keys of an object, this includes not only the keys of the object, but also keys contained in the prototype of that object. It is generally a best practice to check for these keys specifically:

var someObject;
for (var key in someObject) {
    if ( ! someObject.hasOwnProperty(key)) {
        continue; // Skip keys from the prototype.
    }

    doSomethingWith(key);
}
Loading history...
106
                var option = document.createElement("option");
107
                if (useLookupValueAsOptionValue === true) {
108
                    option.value = lookupList[item].value;
109
                } else {
110
                    option.value = lookupList[item].id;
111
                }
112
                option.innerHTML = lookupList[item].value;
113
                selectElem.appendChild(option);
114
            }
115
        } else {
116
            LookupAPI.getLookupData(lookupType, locale, function (request) {
0 ignored issues
show
Unused Code introduced by
The parameter request is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
117
                if (LookupAPI.lookupMap[locale][lookupType]) {
118
                    LookupAPI.populateDropdown(lookupType, elementId, useLookupValueAsOptionValue);
119
                }
120
            });
121
        }
122
    }
123
124
};
125
126
//Populates select elements passed directly
127
LookupAPI.populateDropdownElement = function (lookupType, element, useLookupValueAsOptionValue) {
128
    if (element) {
129
        var locale = TalentCloudAPI.getLanguageFromCookie();
0 ignored issues
show
Bug introduced by
The variable TalentCloudAPI seems to be never declared. If this is a global, consider adding a /** global: TalentCloudAPI */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
130
        if (!LookupAPI.lookupMap[locale]) {
131
            LookupAPI.lookupMap[locale] = {};
132
        }
133
        var lookupList = LookupAPI.lookupMap[locale][lookupType];
134
        if (lookupList) {
135
            Utilities.clearSelectOptions(element);
0 ignored issues
show
Bug introduced by
The variable Utilities seems to be never declared. If this is a global, consider adding a /** global: Utilities */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
136
            for (var item in lookupList) {
0 ignored issues
show
Complexity introduced by
A for in loop automatically includes the property of any prototype object, consider checking the key using hasOwnProperty.

When iterating over the keys of an object, this includes not only the keys of the object, but also keys contained in the prototype of that object. It is generally a best practice to check for these keys specifically:

var someObject;
for (var key in someObject) {
    if ( ! someObject.hasOwnProperty(key)) {
        continue; // Skip keys from the prototype.
    }

    doSomethingWith(key);
}
Loading history...
137
                var option = document.createElement("option");
138
                if (useLookupValueAsOptionValue === true) {
139
                    option.value = lookupList[item].value;
140
                } else {
141
                    option.value = lookupList[item].id;
142
                }
143
                option.innerHTML = lookupList[item].value;
144
                element.appendChild(option);
145
            }
146
        } else {
147
            LookupAPI.getLookupData(lookupType, locale, function (request) {
0 ignored issues
show
Unused Code introduced by
The parameter request is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
148
                if (LookupAPI.lookupMap[locale][lookupType]) {
149
                    LookupAPI.populateDropdownElement(lookupType, element, useLookupValueAsOptionValue);
150
                }
151
            });
152
        }
153
    }
154
};